---
title: "How Politically Savvy are Foreign Portfolio Stock Investors?"
author: "Seungjun Kim"
date: "7/6/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

### Main Analyses
```{r, results='asis'}
library(foreign);library(stargazer); library(MASS); library(haven)
library(Rmisc); library(RColorBrewer); library(ggplot2);library(readr)

test<- read_dta("FPSIs.dta")

# Main Regression (Table A2, Figure 2)
test1<-lm(n0 ~  parkrelated1 + moonrelated1 + newcons  + newdemo + chaebol + logmarketcap11 + roa11 + debtratio11 + 0, data=test)

test2<-lm(n0f ~ parkrelated1 + moonrelated1 + newcons+  newdemo + chaebol + logmarketcap11 + roa11 + debtratio11 + 0, data=test)

stargazer(test1, test2)

m1coef<-summary(test1)
m2coef<-summary(test2)

coeff <- factor(8:1,labels=c("Debt Ratio", "Return on Asset","Firm Size","Chaebol","Left Party", "Right Party", "Moon Connection", "Park Connection"))

model1Frame <- data.frame(Variable = coeff,
                          Coefficient = m1coef$coefficients[,1],
                          SE = m1coef$coefficients[,2],
                          DependentVariable = "Domestic Stock Prices")
model2Frame <- data.frame(Variable = coeff,
                          Coefficient = m2coef$coefficients[,1],
                          SE = m2coef$coefficients[,2],
                          DependentVariable = "Foreign Investors' Buying")

interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier

allModelFrame <- data.frame(rbind(model1Frame, model2Frame)) 

main1<- ggplot(allModelFrame, aes(colour=DependentVariable)) + scale_colour_manual(values = c("black","darkred"))
main1 <- main1 + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
main1 <- main1 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*interval1,
                                ymax = Coefficient + SE*interval1),
                            lwd = 2, position = position_dodge(width = 1/2))
main1 <- main1 + geom_pointrange(aes(x = Variable, y = Coefficient, shape=DependentVariable, ymin = Coefficient - SE*interval2, ymax = Coefficient + SE*interval2),
                             lwd = 1, position = position_dodge(width = 1/2),
                             fill = "WHITE") + scale_shape_manual(values=c(21,19))

main1 <- main1 + coord_flip(ylim = c(-12, 12))  + theme_bw()
main1 <- main1 + ggtitle("Coefficient Plot to Explain Abnormal Returns")

main1 <- main1+ theme(axis.text.y = element_text(face = c('plain', 'plain', 'plain', 'plain', 'bold', 'bold')))

print(main1)

```


```{r, results='asis'}
# Main Regression (Table A3, Figure 2)

declocalappendix<-lm(n0 ~ cconnection + dconnection + chaebol + logmarketcap11 + roa11 + debtratio11 + 0, data=test)

decforeignappendix<-lm(n0f ~ cconnection + dconnection + chaebol + logmarketcap11 + roa11 + debtratio11 + 0, data=test)

stargazer(declocalappendix, decforeignappendix)

m1coef<-summary(declocalappendix)
m2coef<-summary(decforeignappendix)

coeff <- factor(6:1,labels=c("Debt Ratio", "Return on Asset","Firm Size","Chaebol","DUP Connection", "NFP Connection"))

model1Frame <- data.frame(Variable = coeff,
                          Coefficient = m1coef$coefficients[,1],
                          SE = m1coef$coefficients[,2],
                          DependentVariable = "Domestic Stock Prices")
model2Frame <- data.frame(Variable = coeff,
                          Coefficient = m2coef$coefficients[,1],
                          SE = m2coef$coefficients[,2],
                          DependentVariable = "FPSI Prices")

interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier

allModelFrame <- data.frame(rbind(model1Frame, model2Frame)) 

main1appendix<- ggplot(allModelFrame, aes(colour=DependentVariable)) + scale_colour_manual(values = c("black","darkred"))
main1appendix <- main1appendix + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
main1appendix <- main1appendix + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*interval1,
                                ymax = Coefficient + SE*interval1),
                            lwd = 2, position = position_dodge(width = 1/2))
main1appendix <- main1appendix + geom_pointrange(aes(x = Variable, y = Coefficient, shape=DependentVariable, ymin = Coefficient - SE*interval2, ymax = Coefficient + SE*interval2),
                             lwd = 1, position = position_dodge(width = 1/2),
                             fill = "WHITE") + scale_shape_manual(values=c(21,19))

main1appendix <- main1appendix + coord_flip() + theme_bw()
main1appendix <- main1appendix + ggtitle("Coefficient Plot to Explain Abnormal Returns")+ theme(axis.text.y = element_text(face = c('plain', 'plain', 'plain', 'plain', 'bold', 'bold')))

print(main1appendix)

```


```{r, results='asis'}
# Figure 3
library(readxl)
coefficient_domestic <- read_excel("figure3.xlsx")

parkabnormalovertime <- ggplot(coefficient_domestic, aes(x=date , y=parkabnormals)) +ylim(-5,7)  + geom_line(aes(x=date, y=down), colour="grey50", linetype="dashed", size=1)+ geom_line(aes(x=date, y=upper), colour="grey50", linetype="dashed", size=1)+ geom_line(size=2)+ theme_bw()+ geom_vline(xintercept = 0,color = "red3", size=1)

parkabnormalovertime <- parkabnormalovertime + ggtitle("") +
  xlab("") + ylab("Domestic Price Abnormal Return (%)")

parkabnormalovertime <- parkabnormalovertime + scale_x_continuous(breaks=c(-10, -5, 0,6), labels=c("12/5/2012",  "12/12/2012", "12/20/2012", "1/4/2013"))

moonabnormalovertime <- ggplot(coefficient_domestic, aes(x=date , y=moonabnormals)) +ylim(-11,7)  + geom_line(aes(x=date, y=moond), colour="grey50", linetype="dashed", size=1)+ geom_line(aes(x=date, y=moonup), colour="grey50", linetype="dashed", size=1)+ geom_line(size=2)+ theme_bw()+ geom_vline(xintercept = 0,color = "red3", size=1)

moonabnormalovertime <- moonabnormalovertime + ggtitle("") +
  xlab("") + ylab("Domestic Price Abnormal Return (%)")

moonabnormalovertime <- moonabnormalovertime + scale_x_continuous(breaks=c(-10, -5, 0,6), labels=c("12/5/2012",  "12/12/2012", "12/20/2012", "1/4/2013"))

print (parkabnormalovertime)
print (moonabnormalovertime)

```

```{r, results='asis'}
# figure 4
coefficient_foreign <- read_excel("coefficient_foreign.xlsx")

foreignabnormalpark <-ggplot(coefficient_foreign, aes(x=date , y=parkabnormals)) +ylim(-3,4)  + geom_line(aes(x=date, y=down), colour="grey50", linetype="dashed", size=1)+ geom_line(aes(x=date, y=upper), colour="grey50", linetype="dashed", size=1)+ geom_line(size=2)+ theme_bw()+ geom_vline(xintercept = 0,color = "red3", size=1)


foreignabnormalmoon <-ggplot(coefficient_foreign, aes(x=date , y=moonabnormals)) +ylim(-5,5)  + geom_line(aes(x=date, y=moond), colour="grey50", linetype="dashed", size=1)+ geom_line(aes(x=date, y=moonup), colour="grey50", linetype="dashed", size=1)+ geom_line(size=2)+ theme_bw()+ geom_vline(xintercept = 0,color = "red3", size=1)


foreignabnormalpark <- foreignabnormalpark + ggtitle("") +
  xlab("") + ylab("Foreigner Abnormal Buying (hundred, %)")

foreignabnormalmoon <- foreignabnormalmoon + ggtitle("") +
  xlab("") + ylab("Foreigner Abnormal Buying (hundred, %)")

foreignabnormalpark <- foreignabnormalpark + scale_x_continuous(breaks=c(-10, -5, 0,6), labels=c("12/5/2012",  "12/12/2012", "12/20/2012", "1/4/2013"))


foreignabnormalmoon <- foreignabnormalmoon + scale_x_continuous(breaks=c(-10, -5, 0,6), labels=c("12/5/2012",  "12/12/2012", "12/20/2012", "1/4/2013"))

print (foreignabnormalpark)
print (foreignabnormalmoon)
```

### appendix table
```{r, results='asis'}
declocalfull<-lm(n0 ~ parkrelated1 + moonrelated1 + chaebol + logmarketcap11 + roa11 + debtratio11 + as.factor(sic_category) +0, data=test)

decforeignfull<-lm(n0f ~ parkrelated1 + moonrelated1 + chaebol + logmarketcap11 + roa11 + debtratio11 + as.factor(sic_category) +0 , data=test)

stargazer(declocalfull, decforeignfull)

m5coef<-summary(declocalfull)
m6coef<-summary(decforeignfull)

library(Rmisc); library(RColorBrewer); library(ggplot2)
coeff <- factor(15:1,labels=c("Services", "Finance, Insurance and Real Estate", "Retail Trade", "Wholesale Trade", "Transportation and Services","Manufacturing", "Construction", "Mining", "Agriculture, Forestry, And Fishing", "Debt Ratio", "Return on Asset","Firm Size","Chaebol","Moon Connection", "Park Connection"))

model5Frame <- data.frame(Variable = coeff,
                          Coefficient = m5coef$coefficients[,1],
                          SE = m5coef$coefficients[,2],
                          DependentVariable = "Domestic Stock Returns")
model6Frame <- data.frame(Variable = coeff,
                          Coefficient = m6coef$coefficients[,1],
                          SE = m6coef$coefficients[,2],
                          DependentVariable = "FPSI Returns")

interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier

allModelFrame3 <- data.frame(rbind(model5Frame, model6Frame))

main3<- ggplot(allModelFrame3, aes(colour=DependentVariable)) + scale_colour_manual(values = c("black","darkred"))

main3 <- main3 + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
main3 <- main3 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*interval1,
                                ymax = Coefficient + SE*interval1),
                            lwd = 2, position = position_dodge(width = 1/2))
main3 <- main3 + geom_pointrange(aes(x = Variable, y = Coefficient, shape=DependentVariable, ymin = Coefficient - SE*interval2, ymax = Coefficient + SE*interval2),
                             lwd = 1, position = position_dodge(width = 1/2),
                             fill = "WHITE") + scale_shape_manual(values=c(21,19))

main3 <- main3 + coord_flip(ylim = c(-10, 7)) + theme_bw()
main3 <- main3 + ggtitle("Coefficient Plot to Explain Abnormal Returns")+ theme(axis.text.y = element_text(face = c(rep('plain', 13), c(rep('bold',2)))))

print(main3)

```

